1207C - Gas Pipeline - CodeForces Solution


dp greedy *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ll T;
	cin>>T;
	while(T--){
		ll n,a,b;
		scanf("%lld%lld%lld",&n,&a,&b);
		string s;
		cin>>s;
		ll ans=0;
		ll node=0,cnt=0;
		for(int i=0;i<n;i++){
			if(s[i]=='0')
				cnt++;
			else
				break;
		}
		if(cnt!=n)
			ans=(cnt+1)*a+(cnt+2)*b;
		else{
			ans=(n+1)*b+n*a;
			cout<<ans<<endl;
			continue;
		}
		
//		cout<<ans<<endl;
		
		ll cnt1=n-1;
		for(int i=n-1;i>=cnt;i--){
			if(s[i]=='0'){
				cnt1--;
			}
			else
				break;
		}
		ans+=(n-1-cnt1)*b+(n-cnt1)*a;
		
//		cout<<ans<<endl;
		
		ll sum=0;
		for(int i=cnt;i<=cnt1;i++){
			if(s[i]=='0')
				sum++;
			else{
				if(sum==1){
					ans+=a+2*b;
					ans+=a+2*b;
					sum=0;
					continue;
				}
				if(sum==0){
					ans+=a+2*b;
					sum=0;
					continue;
				}
				if((sum-1)*b>2*a){
					ans+=(sum-1)*b;
					ans+=(sum+2)*a;
					ans+=2*b;
					ans+=a+2*b;
				}
				else{
					ans+=(sum+1)*a;
					ans+=(sum+1)*2*b;
				}
				sum=0;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

  					   	   			  	  	  		 	 	


Comments

Submit
0 Comments
More Questions

688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization